using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace Waaahsabi.Controls.TheGame
{
	public class MailControl : Control
	{
		private Button _btnComposeMail;
		private Button _btnCreateAccount;
		private Button _btnDelete;
		private Button _btnForward;
		private Button _btnReply;
		private Button _btnSpam;
		private ComboBox _cmbChooseAccount;
		private DataGrid _dgrdMailHeaders;
		private TreeView _treeView;
		private TreeViewItem _tviInbox;
		private TreeViewItem _tviSent;
		private TreeViewItem _tviSpam;
		private TreeViewItem _tviTrash;
		private TextBox _txtUsername;

		public MailControl()
		{
			DefaultStyleKey = typeof (MailControl);
		}

		public MailCreationControl MailCreationControl { get; private set; }
		public event EventHandler<MailCreateAccountEventArgs> CreateAccount;
		public event EventHandler<MailAccountSelectedEventArgs> MailAccountSelected;
		public event EventHandler<MailFolderSelectedEventArgs> MailFolderSelected;
		public event EventHandler<MailViewEventArgs> MailSelected;
		public event EventHandler SendMail;
		public event EventHandler<ModifyMailEventArgs> DeleteMail;
		public event EventHandler<ModifyMailEventArgs> MarkMailAsSpam;

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			#region treeview

			_treeView = GetTemplateChild("treeView") as TreeView;
			if (_treeView != null)
			{
				TreeViewItem root = _treeView.Items[0] as TreeViewItem;
				if (root != null)
				{
					root.IsExpanded = true;
					_tviInbox = GetTemplateChild("tviInbox") as TreeViewItem;
					if (_tviInbox != null)
					{
						_tviInbox.MouseLeftButtonUp += delegate
						{
							if (MailFolderSelected != null)
								MailFolderSelected(this, new MailFolderSelectedEventArgs {FolderType = MailFolderType.Inbox});
						};
					}
					_tviSent = GetTemplateChild("tviSent") as TreeViewItem;
					if (_tviSent != null)
					{
						_tviSent.MouseLeftButtonUp += delegate
						{
							if (MailFolderSelected != null)
								MailFolderSelected(this, new MailFolderSelectedEventArgs {FolderType = MailFolderType.Sent});
						};
					}
					_tviSpam = GetTemplateChild("tviSpam") as TreeViewItem;
					if (_tviSpam != null)
					{
						_tviSpam.MouseLeftButtonUp += delegate
						{
							if (MailFolderSelected != null)
								MailFolderSelected(this, new MailFolderSelectedEventArgs {FolderType = MailFolderType.Spam});
						};
					}
					_tviTrash = GetTemplateChild("tviTrash") as TreeViewItem;
					if (_tviTrash != null)
					{
						_tviTrash.MouseLeftButtonUp += delegate
						{
							if (MailFolderSelected != null)
								MailFolderSelected(this, new MailFolderSelectedEventArgs {FolderType = MailFolderType.Trash});
						};
					}
				}
			}

			#endregion

			_btnComposeMail = GetTemplateChild("btnComposeMail") as Button;
			if (_btnComposeMail != null)
			{
				_btnComposeMail.Click += delegate { VisualStateManager.GoToState(this, "Composing", true); };
			}

			_btnReply = GetTemplateChild("btnReply") as Button;
			if (_btnReply != null)
			{
				_btnReply.Click += delegate
				{
					//put selected mail's sender into new mail's recipient
					//put selected mail's text into new mail's text
					if (_dgrdMailHeaders.SelectedItem == null || !(_dgrdMailHeaders.SelectedItem is MailControlMailHeader))
						return; //can't reply when no mail selected
					MailControlMailHeader header = _dgrdMailHeaders.SelectedItem as MailControlMailHeader;
					MailCreationControl.To = header.SenderAddress;
					MailCreationControl.Subject = "RE: " + header.Subject;
					MailCreationControl.Body = string.Format("\r\n\r\n[Quote]\r\n{0} wrote at {1}:\r\n{2}\r\n[/Quote]",
					                                         header.SenderAddress, header.SentTime, MailView.Body);
					VisualStateManager.GoToState(this, "Composing", true);
				};
			}

			_btnForward = GetTemplateChild("btnForward") as Button;
			if (_btnForward != null)
			{
				_btnForward.Click += delegate
				{
					if (_dgrdMailHeaders.SelectedItem == null || !(_dgrdMailHeaders.SelectedItem is MailControlMailHeader))
						return; //can't reply when no mail selected
					MailControlMailHeader header = _dgrdMailHeaders.SelectedItem as MailControlMailHeader;
					MailCreationControl.Subject = "FW: " + header.Subject;
					MailCreationControl.Body = string.Format("\r\n\r\n[Quote]\r\n{0} wrote at {1}:\r\n{2}\r\n[/Quote]",
					                                         header.SenderAddress, header.SentTime, MailView.Body);
					VisualStateManager.GoToState(this, "Composing", true);
				};
			}

			_btnDelete = GetTemplateChild("btnDelete") as Button;
			if (_btnDelete != null)
			{
				_btnDelete.Click += delegate
				{
					if (_dgrdMailHeaders.SelectedItem == null || !(_dgrdMailHeaders.SelectedItem is MailControlMailHeader))
						return; //can't delete when no mail selected
					MailControlMailHeader hdr = _dgrdMailHeaders.SelectedItem as MailControlMailHeader;
					if (DeleteMail != null)
						DeleteMail(this, new ModifyMailEventArgs {HeaderId = hdr.Id});
					MailView.Reset();
				};
			}

			_btnSpam = GetTemplateChild("btnSpam") as Button;
			if (_btnSpam != null)
			{
				_btnSpam.Click += delegate
				{
					if (_dgrdMailHeaders.SelectedItem == null || !(_dgrdMailHeaders.SelectedItem is MailControlMailHeader))
						return; //can't delete when no mail selected
					MailControlMailHeader hdr = _dgrdMailHeaders.SelectedItem as MailControlMailHeader;
					if (MarkMailAsSpam != null)
						MarkMailAsSpam(this, new ModifyMailEventArgs {HeaderId = hdr.Id});
					MailView.Reset();
				};
			}

			_cmbChooseAccount = GetTemplateChild("cmbChooseAccount") as ComboBox;
			if (_cmbChooseAccount != null)
			{
				_cmbChooseAccount.SelectionChanged += delegate
				{
					if (MailAccountSelected != null)
					{
						MailControlAccountInfo info = _cmbChooseAccount.SelectedItem as MailControlAccountInfo;
						if (info != null)
							MailAccountSelected(this, new MailAccountSelectedEventArgs {AccountId = info.Id});
					}
				};
			}
			_dgrdMailHeaders = GetTemplateChild("dgrdMailHeaders") as DataGrid;
			if (_dgrdMailHeaders != null)
			{
				_dgrdMailHeaders.SelectionChanged += delegate
				{
					if (_dgrdMailHeaders.SelectedItems.Count > 1)
						return;
					MailControlMailHeader header = _dgrdMailHeaders.SelectedItem as MailControlMailHeader;
					if (header == null)
						return;
					if (MailSelected != null)
						MailSelected(this, new MailViewEventArgs {MessageId = header.Id});
				};
			}
			_txtUsername = GetTemplateChild("txtUsername") as TextBox;
			_btnCreateAccount = GetTemplateChild("btnCreateAccount") as Button;
			if (_btnCreateAccount != null)
			{
				_btnCreateAccount.Click += delegate
				{
					if (CreateAccount != null)
						CreateAccount(this, new MailCreateAccountEventArgs {Username = _txtUsername.Text});
				};
			}

			MailCreationControl = GetTemplateChild("mailCreationControl") as MailCreationControl;
			if (MailCreationControl != null)
			{
				MailCreationControl.SendMail += delegate
				{
					VisualStateManager.GoToState(this, "Normal", true);
					if (SendMail != null)
						SendMail(this, null);
				};
				MailCreationControl.Back += delegate { VisualStateManager.GoToState(this, "Normal", true); };
			}
		}

		public void SetHacked(bool isHacked)
		{
			IsHacked = isHacked;
			VisualStateManager.GoToState(this, isHacked ? "Hacked" : "Normal", false);
		}

		public void SelectFolderItem(MailFolderType folder)
		{
			if (_tviInbox == null || _tviSent == null || _tviSpam == null || _tviTrash == null)
				return;

			switch (folder)
			{
			case MailFolderType.Inbox:
				_tviInbox.IsSelected = true;
				break;
			case MailFolderType.Sent:
				_tviSent.IsSelected = true;
				break;
			case MailFolderType.Spam:
				_tviSpam.IsSelected = true;
				break;
			case MailFolderType.Trash:
				_tviTrash.IsSelected = true;
				break;
			}
		}

		public void ResetComposer()
		{
			MailCreationControl.Reset();
		}

		#region MailAccounts DependencyProperty

		public static readonly DependencyProperty MailAccountsProperty =
			DependencyProperty.Register
				(
					"MailAccounts",
					typeof (ObservableCollection<MailControlAccountInfo>),
					typeof (MailControl),
					new PropertyMetadata(null)
				);

		public ObservableCollection<MailControlAccountInfo> MailAccounts
		{
			get { return (ObservableCollection<MailControlAccountInfo>) GetValue(MailAccountsProperty); }
			set { SetValue(MailAccountsProperty, value); }
		}

		#endregion

		#region MailHeaders DependencyProperty

		public static readonly DependencyProperty MailHeadersProperty =
			DependencyProperty.Register
				(
					"MailHeaders",
					typeof (ObservableCollection<MailControlMailHeader>),
					typeof (MailControl),
					new PropertyMetadata(null)
				);

		public ObservableCollection<MailControlMailHeader> MailHeaders
		{
			get { return (ObservableCollection<MailControlMailHeader>) GetValue(MailHeadersProperty); }
			set { SetValue(MailHeadersProperty, value); }
		}

		#endregion

		#region HostNameAddress DependencyProperty

		public static readonly DependencyProperty HostNameAddressProperty =
			DependencyProperty.Register
				(
					"HostNameAddress",
					typeof (string),
					typeof (MailControl),
					new PropertyMetadata(null)
				);

		public string HostNameAddress
		{
			get { return (string) GetValue(HostNameAddressProperty); }
			set { SetValue(HostNameAddressProperty, value); }
		}

		#endregion

		#region IsHacked DependencyProperty

		public static readonly DependencyProperty IsHackedProperty =
			DependencyProperty.Register
				(
					"IsHacked",
					typeof (bool),
					typeof (MailControl),
					new PropertyMetadata(false)
				);

		public bool IsHacked
		{
			get { return (bool) GetValue(IsHackedProperty); }
			set { SetValue(IsHackedProperty, value); }
		}

		#endregion

		#region MailView DependencyProperty

		public static readonly DependencyProperty MailViewProperty =
			DependencyProperty.Register
				(
					"MailView",
					typeof (MailViewControl),
					typeof (MailControl),
					new PropertyMetadata(null)
				);

		public MailViewControl MailView
		{
			get { return (MailViewControl) GetValue(MailViewProperty); }
			set { SetValue(MailViewProperty, value); }
		}

		#endregion
	}
}